package com.carbon.system.service.impl;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.carbon.common.exception.CommonBizException;
import com.carbon.common.service.BaseServiceImpl;
import com.carbon.system.entity.DataPanel;
import com.carbon.system.entity.SysAccount;
import com.carbon.system.entity.SysTenant;
import com.carbon.system.service.*;
import com.carbon.system.vo.*;
import com.carbon.system.mapper.DataPanelMapper;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;


/**
 * <p>
 * 数据面板 服务实现类
 * </p>
 *
 * @author Li Jun
 * @since 2021-07-20
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class DataPanelServiceImpl extends BaseServiceImpl<DataPanelMapper, DataPanel> implements DataPanelService {

    @Override
    public StatHomeDataVo getHomeData() {
        try {
            // 从数据库查询数据面板数据，取第一条记录
            DataPanel dataPanel = this.getOne(Wrappers.lambdaQuery(DataPanel.class)
                    .orderByDesc(DataPanel::getId)
                    .last("LIMIT 1"));
            
            if (dataPanel != null && dataPanel.getHomeData() != null) {
                log.info("从数据库获取首页数据面板数据成功");
                return dataPanel.getHomeData();
            } else {
                log.warn("数据库中没有找到首页数据面板数据，返回空数据");
                return new StatHomeDataVo();
            }
        } catch (Exception e) {
            log.error("获取首页数据面板数据失败", e);
            return new StatHomeDataVo();
        }
    }
}
